<?php

namespace app\bll;

use app\validate\MsgTemplate as ValidateMsgTemplate;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use support\Db;


/**
 * 消息模板
 */
class MsgTemplate
{

    /**
     * 获取单个模板
     *
     * @param integer $id
     * @return array
     */
    public function find(int $id): array
    {
        $data = Db::table('frame_msg_template')
            ->where('id', '=', $id)
            ->first();
        return $data === null ? [] : get_object_vars($data);
    }

    /**
     * 根据HOOKIDENT查询
     *
     * @param string $hookident
     * @return array
     */
    public function selectByHookIdent(string $hookident): array
    {
        return Db::table('frame_msg_template')
            ->where('hookident', '=', $hookident)
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }

    /**
     * 根据发送通道查询
     *
     * @param integer $id
     * @return array
     */
    public function selectByChannelid(int $id): array
    {
        return  Db::table('frame_msg_template')
            ->where('channel', 'like', ',' . $id . ',')
            ->orderByRaw('id desc')
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }

    /**
     * 添加
     *
     * @param array $data
     * @return integer
     */
    public function add(array $data): int
    {
        $ValidateMsgTemplate = new ValidateMsgTemplate();
        if (!$ValidateMsgTemplate->check($data)) {
            throw new Exception($ValidateMsgTemplate->getSimpleError());
        }

        $data['channel'] = ',' . implode(',', $data['channel']) . ',';
        return Db::table('frame_msg_template')
            ->insertGetId($data);
    }


    /**
     * 编辑
     *
     * @param integer $id
     * @param array $data
     * @return void
     */
    public function edit(int $id, array $data)
    {
        $ValidateMsgTemplate = new ValidateMsgTemplate();
        if (!$ValidateMsgTemplate->check($data)) {
            throw new Exception($ValidateMsgTemplate->getSimpleError());
        }

        $data['channel'] = ',' . implode(',', $data['channel']) . ',';

        return Db::table('frame_msg_template')
            ->where('id', '=', $id)
            ->update($data);
    }

    /**
     * 删除
     *
     * @param integer $id
     * @return void
     */
    public function delete(int $id)
    {
        return  Db::table('frame_msg_template')
            ->where('id', '=', $id)
            ->delete();
    }




    /**
     * 分页查询
     *
     * @param integer $pagesize
     * @return LengthAwarePaginator
     */
    public function paginate(
        string $title = '',
        int $pagesize = 20
    ): LengthAwarePaginator {
        return  Db::table('frame_msg_template')
            ->when($title !== '', function ($query) use ($title) {
                return $query->where('title', 'like', '%' . $title . '%');
            })
            ->orderBy('id', 'desc')
            ->paginate($pagesize);
    }
}
